package com.agileplex.codeminer.commons.props;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * Utility class for properties
 * 
 * @creator Weiming Yin
 * @date 2007-7-17
 *
 * @lastmodified
 * @author $Author: yinweiming $
 * @version $Revision: 15 $, $Date: 2007-07-16 18:12:59 +0000 (Mon, 16 Jul 2007) $
 */
public class PropsUtil {
	static Logger logger = Logger.getLogger(PropsUtil.class);
	
	static boolean useCache = false;
	static Map<String, Properties> propsMap = new HashMap<String, Properties>();
	static {
		useCache = "true".equalsIgnoreCase(System.getProperty("use.cache"));
		
	}
	
	public static String getString(String fileName, String key) {
		Properties prop = null;
		if (useCache) {
			prop = propsMap.get(fileName);
		}
		
		if (prop == null) {
			try {
				prop = Properties.loadProperties(PropsUtil.class.getClassLoader().getResourceAsStream(fileName));
				if (useCache) {
					propsMap.put(fileName, prop);
				}
				
				return prop.get(key);
			} catch (IOException e) {
				logger.error(e);
			}
		}
		
		return null;
	}
}
